<template>
  <div class="profile">
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header>
        <el-menu mode="horizontal" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" router>
          <div class="logo">
            <i class="el-icon-reading"></i>
            校园二手书交易平台
          </div>
          <el-menu-item index="/home">首页</el-menu-item>
          <el-menu-item index="/market">书籍市场</el-menu-item>
          <el-menu-item index="/my-posts">我的发布</el-menu-item>
          <el-menu-item index="/messages">消息中心</el-menu-item>
        </el-menu>
      </el-header>

      <el-main>
        <div class="profile-container">
          <h2>个人信息</h2>
          <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px">
            <el-form-item label="头像" prop="avatar">
              <el-upload
                class="avatar-uploader"
                action="#"
                :show-file-list="false"
                :before-upload="beforeAvatarUpload"
                :http-request="uploadAvatar"
              >
                <img v-if="userForm.avatar" :src="userForm.avatar" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="用户名" prop="username">
              <el-input v-model="userForm.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="userForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="userForm.email"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">保存修改</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'ProfilePage',
  data() {
    return {
      userForm: {
        id: '',
        username: '',
        avatar: '',
        phone: '',
        email: '',
        createTime: '',
        updateTime: ''
      },
      rules: {
        phone: [
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        email: [
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      try {
        const res = await request.get('/api/home/user/info')
        if (res.data.code === 1) {
          this.userForm = res.data.data
        } else {
          this.$message.error(res.data.msg || '获取用户信息失败')
        }
      } catch (error) {
        this.$message.error('获取用户信息失败')
      }
    },
    // 头像上传前的验证
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 自定义头像上传方法
    async uploadAvatar(params) {
      try {
        const formData = new FormData()
        formData.append('file', params.file)
        const res = await request.post('/api/upload/avatar', formData)
        if (res.data.code === 1) {
          this.userForm.avatar = res.data.data
          this.$message.success('头像上传成功')
        } else {
          this.$message.error(res.data.msg || '头像上传失败')
        }
      } catch (error) {
        this.$message.error('头像上传失败')
      }
    },
    // 提交表单
    async submitForm() {
      try {
        await this.$refs.userForm.validate()
        const res = await request.put('/api/home/user/update', this.userForm)
        if (res.data.code === 1) {
          this.$message.success('保存成功')
        } else {
          this.$message.error(res.data.msg || '保存失败')
        }
      } catch (error) {
        this.$message.error('保存失败')
      }
    }
  },
  created() {
    this.getUserInfo()
  }
}
</script>

<style scoped>
.profile {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
}

.el-header {
  padding: 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background: linear-gradient(to right, #545c64, #4a4f55);
}

.logo {
  float: left;
  line-height: 60px;
  margin: 0 20px;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  display: flex;
  align-items: center;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}

.logo i {
  margin-right: 10px;
  font-size: 24px;
  color: #ffd04b;
}

.profile-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 30px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  animation: fadeIn 0.5s ease-out;
  transition: all 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.profile-container:hover {
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
}

h2 {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
  color: #303133;
  font-size: 24px;
  font-weight: 600;
  background: linear-gradient(45deg, #409EFF, #36D1DC);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.avatar-uploader {
  border: 2px dashed #d9d9d9;
  border-radius: 12px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px;
  height: 178px;
  transition: all 0.3s ease;
}

.avatar-uploader:hover {
  border-color: #409EFF;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.1);
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  transition: all 0.3s ease;
}

.avatar-uploader:hover .avatar-uploader-icon {
  color: #409EFF;
  transform: scale(1.1);
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
  object-fit: cover;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.avatar:hover {
  transform: scale(1.05);
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
}

:deep(.el-input__inner) {
  height: 40px;
  line-height: 40px;
  border-radius: 8px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s ease;
  padding-left: 15px;
}

:deep(.el-input__inner:focus) {
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

:deep(.el-button--primary) {
  height: 40px;
  padding: 0 20px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(45deg, #409EFF, #36D1DC);
  border: none;
  transition: all 0.3s ease;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
}

:deep(.el-button--primary:active) {
  transform: translateY(0);
}

:deep(.el-form-item.is-error .el-input__inner) {
  border-color: #f56c6c;
  box-shadow: 0 0 0 2px rgba(245, 108, 108, 0.1);
}

:deep(.el-form-item__error) {
  color: #f56c6c;
  font-size: 12px;
  padding-top: 4px;
}

@media (max-width: 768px) {
  .profile-container {
    margin: 10px;
    padding: 20px;
  }

  .avatar-uploader {
    width: 120px;
    height: 120px;
  }

  .avatar-uploader-icon {
    width: 120px;
    height: 120px;
    line-height: 120px;
  }

  .avatar {
    width: 120px;
    height: 120px;
  }

  h2 {
    font-size: 20px;
    margin-bottom: 20px;
  }
}
</style>